/**
 * Created by fq on 2018/1/25 0025.
 */
'use strict';
angular.module('indexApp')
    .factory('searchUtil', function ($http, $rootScope) {
        return {
            /*用于执行在多选项卡点击的时候，实现不同选项卡的choose值的改变*/
            checkboxMulti: function (ind, arr) {
                // if (ind === 0) {
                //     arr.forEach(function (item) {
                //         item.choose = false
                //     });
                //     arr[0].choose = true
                // } else
                {
                    arr[ind].choose = !arr[ind].choose;
                    // arr[0].choose = false;
                    if (arr.every(function (item) {
                        return item.choose === false
                    })) {
                        // arr[0].choose = !arr[0].choose;
                    }
                }
            },
            /*用于执行在发布时间选项卡被点击的时候，执行*/
            radioType: function (ind, arr) {
                arr[ind].choose = !arr[ind].choose;
                arr.forEach(function (item, index) {
                    if (index !== ind) {
                        item.choose = false
                    }
                    else if (arr.every(function (item) {
                        return item.choose == false
                    })) {
                        // arr[0].choose = true;
                    }
                })
            },
            /*判断sessionstorage是有存储的选项卡的值，如果有则使用，以使得页面刷新选定的选项卡会保持*/
            judegesessionStorage: function (judge, data) {
                var dataCopy = angular.copy(data);
                if (judge === undefined) {
                    var option = dataCopy;
                } else {
                    option = JSON.parse(judge);
                }
                return option;
            },
            /*数据转换,将数组数据转换为可以用于传输的字符串*/
            dataConvert: function (data) {
                //转换为字符串
                var asdas = {};
                var dataname;
                for (dataname in data) {
                    asdas[dataname] = data[dataname].filter(function (item, index) {
                        return item.choose === true
                    });
                    asdas[dataname] = asdas[dataname].map(function (item) {
                        return item.type
                    });
                    asdas[dataname] = asdas[dataname].toString()
                }
                return asdas;
            },
            judgeNotFind: function (res) {
                if (res.code < 0 || res.data.length === 0) {
                    return true
                }
            },
            //图片上传,
            uploadFile2: function (formData) {
                return $http.post($rootScope.http + 'upload_image', formData, {
                    withCredentials: true,
                    headers: {'Content-Type': undefined},
                    transformRequest: angular.identity
                })
            }
        }
    })
    .factory('Reddit', function ($http) {
        var Reddit = function () {
            this.items = [];
            this.busy = false;
            this.after = '';
        };

        Reddit.prototype.nextPage = function () {
            if (this.busy) return;
            this.busy = true;

            var url = "http://api.reddit.com/hot?after=" + this.after + "&jsonp=JSON_CALLBACK";
            $http.jsonp(url).success(function (data) {
                var items = data.data.children;
                for (var i = 0; i < items.length; i++) {
                    this.items.push(items[i].data);
                }
                this.after = "t3_" + this.items[this.items.length - 1].id;
                this.busy = false;
            }.bind(this));
        };

        return Reddit;
    })
;